<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\index\BaseController;
use app\index\logic\Token;
use app\index\model\Order;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class User extends BaseController
{
    //订单列表页
    public function user()
    {
        if (isset($_GET['code']) && !empty($_GET['code'])){
            Token::token($_GET['code']);
        }
        $id = Session::get('user');
        $list = Order::orderList($id);
        foreach ($list as $v){
            $porduct = Order::suffix('_product')
                ->alias('op')
                ->field('op.num,p.photo_x')
                ->where('op.order_id','=',$v['id'])
                ->join('product p','p.id=op.pid')
                ->select()->toArray();
            $arr = [];
            $img = [];
            foreach ($porduct as $vv){
                array_push($arr,$vv['num']);
                array_push($img,$vv['photo_x']);
            }
            $v['num'] = $arr;
            $v['photo_x'] = $img;
        }
        View::assign('list',$list);
        return View::fetch();
    }
    //订单详情
    public function userinfo()
    {
        $get = Request::get();
        $find = Order::orderInfo($get['id']);
        View::assign('find',$find);
        $porduct = Order::suffix('_product')
            ->alias('op')
            ->field('op.num,p.photo_x,op.price,op.name')
            ->where('op.order_id','=',$find['id'])
            ->join('product p','p.id=op.pid')
            ->select()->toArray();
        View::assign('porduct',$porduct);
        return View::fetch();
    }
    //退款
    public function backMoney()
    {
        $get = Request::get();
        $order = Order::where('order_sn','=',$get['sn'])->find()->toArray();
        View::assign('order',$order);
        if (Request::isPost()){
            $post = Request::post();
            $edit = [
                'id'=>$order['id'],
                'status'=>21,
                'refundReamrk'=>$post['reamrk'],
                'refundMoney'=>$post['money'],
                'history_status'=>$order['status']
            ];
            $res = Order::update($edit);
            if ($res){
                $this->success('申请成功,请等待',url('User/user'));
            }
        }
        return View::fetch();
    }
    //取消订单
    public function ajaxquxiao()
    {
        $post = Request::post();
        $edit = [
            'id'=>$post['sid'],
            'status'=>0,
        ];
        try{
            $res = Order::update($edit);
        }catch (\Exception $e){
            $res = false;
        }
        return $res;
    }
    //确认收获
    public function ajaxyes()
    {
        $post = Request::post();
        $edit = [
            'id'=>$post['sid'],
            'status'=>20,
        ];
        try{
            $res = Order::update($edit);
        }catch (\Exception $e){
            $res = false;
        }
        return $res;
    }



}